//
//  PKMusicPlayer.m
//  MusicDemo
//
//  Created by ybq on 15/12/24.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKMusicPlayer.h"
#import <AVFoundation/AVFoundation.h>
@interface PKMusicPlayer ()

@property (nonatomic, strong) AVPlayer *player;    // 音乐播放器


@end

@implementation PKMusicPlayer

+ (instancetype)sharePlayer {
    static PKMusicPlayer *player = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        player = [[PKMusicPlayer alloc] init];
    });
    return player;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _player = [[AVPlayer alloc] init];
     //   _index = 0;
    }
    return self;
}

- (void)setMusicArray:(NSArray *)musicArray {
    _musicArray = musicArray;
    // 相当于给音乐播放器数组赋值的时候,同时会设置播放器默认播放的歌曲为第一首
        AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:_musicArray[_index]]];
        [_player replaceCurrentItemWithPlayerItem:item];
        [_player play];
}

- (void)lastMusic {
    _index--;
    if (_index == -1) {
        _index = _musicArray.count - 1;
    }
    if (_playType == Random) {
        _index = arc4random() % _musicArray.count;
    }
//    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:_musicArray[_index]]];
//    [_player replaceCurrentItemWithPlayerItem:item];
//    [_player play];    // 点了上一首歌就去自动播放
    [self changeMusicWithIndex:_index];
    
}

- (void)nextMusic {
    _index++;
    if (_index == _musicArray.count) {
        _index = 0;
    }
    if (_playType == Random) {
        _index = arc4random() % _musicArray.count;
    }
//    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:_musicArray[_index]]];
//    [_player replaceCurrentItemWithPlayerItem:item];
//    [_player play];    // 点了下一首歌就去自动播放
    [self changeMusicWithIndex:_index];
}

- (void)play {
    [_player play];
}

- (void)pause {
    [_player pause];
}

- (void)seekToTime:(NSInteger)newTime {
    // value/timescale = seconds
    // value = seconds * timescale;
    CMTime currentTime = _player.currentTime;
    currentTime.value = newTime * currentTime.timescale;
    [_player seekToTime:currentTime];
}

- (void)stop {
    [_player pause];
    [self seekToTime:0];
}

- (NSInteger)currentTime{
    if (_player.currentTime.timescale == 0) {
        return 0;
    }
    return _player.currentTime.value / _player.currentTime.timescale;
}

- (NSInteger)finishTime {
    CMTime time = _player.currentItem.duration;
    if (time.timescale == 0) {
        return 0;
    }
    return time.value / time.timescale;
}

- (BOOL)isPlaying {
    // 播放状态 1为play 0为stopped
    if (_player.rate == 1.0) {
        return YES;
    } else {
        return NO;
    }
}

- (void)finishPlay {
    if (_playType == Single) {
        [self seekToTime:0];
    } else {
        [self nextMusic];
    }
}

- (void)changeMusicWithIndex:(NSInteger)index {
    _index = index;
    NSURL *url = [NSURL URLWithString:_musicArray[index]];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
    [_player replaceCurrentItemWithPlayerItem:item];
    [_player play];
}

@end
